package jflowmap.model.ontology;

import java.util.Set;

import com.google.common.collect.Sets;


public abstract class Filter {

	private Filter next;
	private Operator operator;
	
	private enum Operator {
		AND, OR
	};
	
	public final Filter and (Filter next) {
		Filter end = last();
		end.next = next;
		end.operator = Operator.AND;
		return this;
	}
	
	public final Filter or (Filter next) {
		Filter end = last();
		end.next = next;
		end.operator = Operator.OR;
		return this;
	}
	
	private final Filter last () {
		if (next == null) return this;
		else return next.last(); 
	}
	
	public final Set<Value> filter (Set<Value> values) {
		if (next == null)
			return reduce(values);
		
		switch (operator) {
			case OR: return Sets.union(reduce(values), next.filter(values));
			case AND:
			default: return Sets.intersection(reduce(values), next.filter(values));
		}
	}
	
	public abstract Set<Value> reduce (Set<Value> values);
}
